//
//  ZipViewController.swift
//  XMenSwift
//
//  Created by Jason on 2023/8/18.
//

import UIKit
import SSZipArchive

class ZipViewController: UIViewController {
    
    lazy var imageV: UIImageView = {
        let view = UIImageView()
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .red
        self.view.addSubview(imageV)
        imageV.snp.makeConstraints { make in
            make.width.height.equalTo(200)
            make.center.equalToSuperview()
        }
        // Do any additional setup after loading the view.
        let fileName = "zipDocument"
        let resourceName = "LOGO"
        guard let path =  Bundle.main.path(forResource: resourceName, ofType: "zip") else { return}
        let fileWithResource = fileName.appending("/\(resourceName)")
        //判断文件夹是否存在
        if !fileExists(path: getFilePath(fileName: fileWithResource)) {
            if let tempUnzipPath = createFilePath(fileName: fileName) {
                let result = SSZipArchive.unzipFile(atPath: path, toDestination: tempUnzipPath)
                print(result)
                let imagepath = fileWithResource.appending("/findSignum-successLogo")
                let fileName = getFilePath(fileName: imagepath)
                imageV.image = UIImage(contentsOfFile: fileName)
            }
            
        } else {
            //            removeFile(path: getFilePath(fileName: fileWithResource))
            let imagepath = fileWithResource.appending("/findSignum-successLogo")
            let fileName = getFilePath(fileName: imagepath)
            imageV.image = UIImage(contentsOfFile: fileName)
        }
        
        if let path = Bundle.main.path(forResource: "test", ofType: "json") {
            jsonFileToDictionary(path: path)
        }
        
    }
    
    /// 获取文件夹路径
    /// - Parameters:
    ///   - file: 文件名称
    /// - Returns: FilePath
    func getFilePath(fileName: String) -> String {
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        return "\(path)/\(fileName)"
    }
    
    //判断文件夹是否存在
    func fileExists(path: String) -> Bool {
        if FileManager.default.fileExists(atPath: path) {
            return true
        }
        return false
    }
    
    //删除文件
    func removeFile(path: String) -> Bool {
        do {
            try FileManager.default.removeItem(atPath: path)
        } catch  let error as NSError {
            print(error.localizedDescription)
            return false
        }
        return true
    }
    
    /// 创建文件夹
    /// - Parameter fileName: 文件夹名称
    /// - Returns: 文件夹路径
    func createFilePath(fileName: String) -> String? {
        var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        path.append("/\(fileName)")
        //      let url = URL(fileURLWithPath: path)
        do {
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true)
        } catch {
            return nil
        }
        return path
    }
    
    /// jsonFile转字典
    /// - Parameter path: 文件路径
    /// - Returns: Dictionary
    func jsonFileToDictionary(path: String) -> Dictionary<String, Any> {
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
            let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
            if let jsonResult = jsonResult as? Dictionary<String, Any> {
                return jsonResult
            }
        } catch {
            // handle error
            debugPrint("\(error)")
        }
        return Dictionary<String, Any>()
    }
    
    
}
